//
//  PayPlug.m
//  PayDemo
//
//  Created by sam on 15/11/2.
//  Copyright © 2015年 sam. All rights reserved.
//

#import "PayPlug.h"
//---------支付宝---------
#import "Order.h"
#import "RSADataSigner.h"
#import "openssl_wrapper.h"

@interface PayPlug ()

@property (nonatomic, copy)  PayResult block;

@end

@implementation PayPlug

//创建该类的单例
+ (instancetype)sharedInstance {
	static id instace = nil;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		instace = [[self alloc] init];
	});
	return instace;
}

- (void)doPay:(PayResult)block {
	self.block = block;
    [self Alipay];
}

- (void)Alipay {
    Order * order = [[Order alloc]init];
    order.partner = ALIPAYPartnerID;
    order.seller = ALIPAYSellerID;
    order.tradeNO = self.sn;
    order.productName = self.productTitle;
    order.productDescription = self.productDesc;
    order.amount = self.productPrice;
//    order.notifyURL = [NSString stringWithFormat:@"%@%@",BASIC_URL, self.notiUrl];
    order.notifyURL = self.notiUrl;
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
	//将商品信息拼接成字符串
	NSString *orderSpec = [order description];
	NSLog(@"orderSpec = %@", orderSpec);

	//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
	id <DataSigner> signer = CreateRSADataSigner(PrivateKey);
	NSString *signedString = [signer signString:orderSpec];
	//将签名成功字符串格式化为订单字符串,请严格按照该格式
	NSString *orderString = nil;
	if (signedString != nil) {
		orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"", orderSpec, signedString, @"RSA"];
		[[AlipaySDK defaultService] payOrder:orderString fromScheme:AlipayScheme callback: ^(NSDictionary *resultDic) {
            if (resultDic[@"resultStatus"]) {
                switch ([resultDic[@"resultStatus"] integerValue]) {
                    case 6001:
                        if (self.block) {
                            self.block(self.payWay, @"您已取消付款", nil);
                        }
                        break;
                    case 6002:
                        if (self.block) {
                            self.block(self.payWay, @"网络连接出错", nil);
                        }
                        break;
                    case 4000:{
                        NSString * errorStr = [NSString string];
                        if (resultDic[@"memo"]) {
                            errorStr = resultDic[@"memo"];
                        } else {
                            errorStr = @"未知错误";
                        }
                        if (self.block) {
                            self.block(self.payWay, errorStr, nil);
                        }
                        break;
                    }
                    case 8000:
                        if (self.block) {
                            self.block(self.payWay, @"订单正在处理中", nil);
                        }
                        break;
                    case 9000:
                        if (self.block) {
                            self.block(self.payWay, @"支付成功", nil);
                        }
                        break;
                    default:
                        break;
                }
            }
		}];
	}
}
@end
